/**
 * Copyright (C) 2008 GeoScheduler Team, as stated on <http://www.geoscheduler.org/authors>.
 * 
 * This file is part of GeoScheduler.
 * 
 * GeoScheduler is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * GeoScheduler is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with GeoScheduler. If not, see <http://www.gnu.org/licenses/>.
 */

package org.geoscheduler.rest;

import java.util.Collections;
import java.util.List;

import javax.annotation.Resource;
import javax.ws.rs.ConsumeMime;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.ProduceMime;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.geoscheduler.commons.marshaller.MarshallingIterator;
import org.geoscheduler.commons.msgs.ParametrizedMessage;
import org.geoscheduler.commons.msgs.types.GeneralMsgs;
import org.geoscheduler.model.Tag;
import org.geoscheduler.rest.utils.MIMEConstants;
import org.json.JSONObject;

/**
 * 
 * <h2>EventsAccountEidTagResource</h2>
 *
 * @author lko
 */
@Resource
@Path("events/{login}/{eid}/tags")
public class EventsAccountEidTagResource extends AbstractResource {

	public static final Log LOG = LogFactory.getLog(EventsAccountEidTagResource.class);

	@PUT
	@ConsumeMime(MIMEConstants.APP_JSON)
	@ProduceMime(MIMEConstants.APP_JSON)
	public JSONObject putTag(@PathParam("login") String login, @PathParam("eid") Long eid, JSONObject jsonTag) {
		try {
			Tag tag = getMarshaller().unmarshall(new Tag(), jsonTag);
			getTagDAO().insert(tag);
			getEventDAO().addTag(eid, tag.getId());
			return response(tag);
		} catch (Exception ex) {
			LOG.error("failed to put tag " + jsonTag);
			return response(ex);
		}
	}

	@GET
	@ConsumeMime(MIMEConstants.APP_FORM_URLENCODED)
	@ProduceMime(MIMEConstants.APP_JSON)
	public JSONObject putTag(@PathParam("login") String login, @PathParam("eid") Long eid) {
		try {
			List<Tag> tags = getTagDAO().loadByEvent(eid);
			return response(new MarshallingIterator<Tag>(tags.iterator(), getMarshaller()));
		} catch (Exception ex) {
			LOG.error("failed to get tags ", ex);
			return response(ex);
		}
	}

	@POST
	@ConsumeMime(MIMEConstants.APP_JSON)
	@ProduceMime(MIMEConstants.APP_JSON)
	public JSONObject postTag(@PathParam("login") String login, @PathParam("eid") Long eid, JSONObject jsonTag) {
		try {
			Tag tag = getMarshaller().unmarshall(new Tag(), jsonTag);
			return response(getTagDAO().update(tag));
		} catch (Exception ex) {
			LOG.error("failed to update tag " + jsonTag);
			return response(ex);
		}
	}

	@GET
	@Path("{tid}")
	@ConsumeMime(MIMEConstants.APP_FORM_URLENCODED)
	@ProduceMime(MIMEConstants.APP_JSON)
	public JSONObject getTag(@PathParam("login") String login, @PathParam("eid") Long eid, @PathParam("tid") Long tid) {
		try {
			Tag tag = getTagDAO().loadById(tid);
			return response(tag == null ? new ParametrizedMessage(GeneralMsgs.NOT_FOUND, String.valueOf(tid), String
					.valueOf(eid), login) : tag);
		} catch (Exception ex) {
			LOG.error("failed to get tag " + tid);
			return response(ex);
		}
	}

	@DELETE
	@Path("{tid}")
	@ConsumeMime(MIMEConstants.APP_FORM_URLENCODED)
	@ProduceMime(MIMEConstants.APP_JSON)
	public JSONObject removeTag(@PathParam("login") String login, @PathParam("eid") Long eid, @PathParam("tid") Long tid) {
		try {
			int deleted = getContext().getEventDAO().removeTag(eid, tid);
			return response(Collections.singletonMap("deleted", deleted));
		} catch (Exception ex) {
			LOG.error("failed to delete tag " + tid);
			return response(ex);
		}
	}

}
